package com.seres.util;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Tan-Bowen
 * @description TODO
 * @date 2021/7/26 18:23
 */
public class PageUtil {

    /**
     * @param [page, records]
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<T>
     * @author Tan-Bowen
     * @description TODO 自己传入列表构造分页
     * @date 2021/7/26 18:21
     * @paramType [com.baomidou.mybatisplus.core.metadata.IPage, java.util.List<T>]
     */
    public static <T> Page<T> initPage(IPage<?> page, List<T> records) {
        Page<T> responsePage = new Page<>();
        responsePage.setTotal(page.getTotal());
        responsePage.setCurrent(page.getCurrent());
        responsePage.setSize(page.getSize());
        responsePage.setRecords(records);
        return responsePage;
    }

    /**
     * @param [page, clz]
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<T>
     * @author Tan-Bowen
     * @description TODO vo实体和数据库查询出得实体是一致得情况使用此方法进行构造分页
     * @date 2021/7/26 18:21
     * @paramType [com.baomidou.mybatisplus.core.metadata.IPage, java.lang.Class<T>]
     */
    public static <T> Page<T> initPage(IPage<?> page, Class<T> clz) {
        List<T> records = new ArrayList<>();
        for (Object role : page.getRecords()) {
            if (role == null) {
                continue;
            }
            T t = BeanUtil.copyProperties(role, clz);
            records.add(t);
        }
        Page<T> responsePage = new Page<>();
        responsePage.setTotal(page.getTotal());
        responsePage.setCurrent(page.getCurrent());
        responsePage.setSize(page.getSize());
        responsePage.setRecords(records);
        return responsePage;
    }

    @Data
    public static class Pages {

        public Pages(Integer current, Integer size) {
            this.current = current == null ? 1 : current;
            this.size = size == null ? 10 : size;
        }

        @ApiModelProperty("页长")
        private Integer size;

        @ApiModelProperty("页码")
        private Integer current;

    }

}
